package client.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.ObjectInputStream;
import java.io.PrintWriter;

import javax.swing.JFrame;
import javax.swing.JPanel;

import shared.player.Player;
import shared.util.Config;
import client.gui.ErrorMessageGui;

/**
 * The Class GameWindow.
 */
public class GameWindow extends JPanel implements KeyListener {

	
	/** The unique identifier. */
	private static final long serialVersionUID = 5807511511774486977L;
	
	/** The player objects. */
	Player p, opp1, opp2;
	
	/** Traffic in. */
	ObjectInputStream netIn;
	
	/** Traffic out. */
	PrintWriter netOut;
	
	/** The gameWindow. */
	JFrame frame;
	
	/** The size of a players body block. */
	int size;
	
	/** The message sender object for sending messages to the server. */
	MessageSender sender;
	
	/** The configuration object. */
	Config conf;

	/**
	 * Instantiates a new game window.
	 *
	 * @param p the player
	 * @param opp1 the first opponent
	 * @param opp2 the second opponent
	 * @param netIn the inbound traffic
	 * @param netOut the outbound traffic
	 * @param conf the configuration object
	 */
	public GameWindow(Player p, Player opp1, Player opp2, ObjectInputStream netIn, PrintWriter netOut, Config conf) {
		this.p = p;
		this.opp1 = opp1;
		this.opp2 = opp2;
		this.netIn = netIn;
		this.netOut = netOut;
		this.conf = conf;
		size = conf.getSize();
		frame = new JFrame("TronGame");
		frame.setBackground(Color.BLACK);
		frame.setSize(650, 550);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		this.setVisible(true);
		frame.add(this);
		sender = new MessageSender(netOut, p);
		new MessageReceiver(netIn, netOut,opp2, opp1, p, sender, this);
		int wait = conf.getWaitBeforeReady();
		addKeyListener(this);
		this.setFocusable(true);
		repaint();
		try {
			Thread.sleep(wait);
		} catch (InterruptedException e) {
			new ErrorMessageGui("Sleep interrupted");
		}
		sender.sendReady();
	}
	
	/**paint the players head
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	public void paint(Graphics g) {
		g.setColor(Color.WHITE);
		for (int i = 0; i < 100; i++) {
			g.fillRect(0 + i * size, 0, size, size);
			g.fillRect(0 + i * size, 500 - size, size, size);
			g.fillRect(0, 0 + i * size, size, size);
			g.fillRect(500 - size, 0 + i * size, size, size);
		}
		g.setColor(Color.GREEN);
		g.fillRect(p.x * size, p.y  * size, size, size);
		g.drawString(p.name, 550, 100);
		
		g.setColor(Color.RED);
		g.fillRect(opp1.x * size, opp1.y * size, size, size);
		g.drawString(opp1.name, 550, 120);
		
		g.setColor(Color.BLUE);
		g.fillRect(opp2.x * size, opp2.y * size, size, size);
		g.drawString(opp2.name, 550, 140);
	}
	
	/**changes the direction of the players movement
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		switch (keyCode) {
		case KeyEvent.VK_UP:
			if (p.direction != 1) {
				p.setDir(0);
			}
			break;
		case KeyEvent.VK_DOWN:
			if (p.direction != 0) {
				p.setDir(1);
			}
			break;
		case KeyEvent.VK_LEFT:
			if (p.direction != 3) {
				p.setDir(2);
			}
			break;
		case KeyEvent.VK_RIGHT:
			if (p.direction != 2) {
				p.setDir(3);
			}
			break;
		}
	}

	/**what to do when key is released
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	/** what to do when key is typed
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyTyped(KeyEvent arg0) {
	}
	
	/**
	 * Dispose of the gameWindow
	 */
	public void dispose() {
		frame.dispose();
	}
}
